import logging

from xtquant import xtdata

logger = logging.getLogger(__name__)


def get_all_stock_list():
    try:
        # '沪深京A股', '沪深转债', '香港联交所股票'
        result1 = xtdata.get_stock_list_in_sector('沪深京A股') or []
        result2 = xtdata.get_stock_list_in_sector('沪深转债') or []
        result3 = xtdata.get_stock_list_in_sector('香港联交所股票') or []
        result4 = xtdata.get_stock_list_in_sector('沪深ETF') or []
        result5 = xtdata.get_stock_list_in_sector('沪市基金') or []
        result = result1 + result2 + result3 + result4 + result5
        return result
    except Exception as e:
        logger.error(f"获取股票列表时发生错误: {e}")
        return []


def get_stock_data(stock_code):
    try:
        result = xtdata.get_instrument_detail(stock_code, False)
        if result is None:
            logger.warning(f"获取股票 {stock_code} 详细信息返回None")
            return {}
        return result
    except Exception as e:
        logger.error(f"获取股票 {stock_code} 详细信息时发生错误: {e}")
        return {}
